package cn.silver.module.system.domain;

import cn.silver.framework.core.utils.DateUtils;
import cn.silver.framework.mybatis.annotation.Table;
import cn.silver.framework.mybatis.annotation.TableField;
import cn.silver.framework.mybatis.domain.BaseEntity;
import cn.silver.framework.mybatis.enums.SearchEnums;
import cn.silver.module.system.enums.DeptPostTypeEnums;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serial;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

/**
 * 用户和岗位关联 sys_user_post
 *
 * @author Silver.Zhou
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "sys_dept_user")
@EqualsAndHashCode(callSuper = true)
@Schema(name = "SysDeptUser", description = "用户和部门关联")
public class SysDeptUser extends BaseEntity {

	@Serial
	private static final long serialVersionUID = 1L;
	/**
	 * 部门Id
	 */
	@Schema(title = "部门Id")
	@TableField(name = "dept_id", searchType = SearchEnums.EQ)
	private String deptId;
	/**
	 * 用户ID
	 */
	@Schema(title = "人员ID")
	@TableField(name = "user_id", searchType = SearchEnums.EQ)
	private String userId;
	
	@Schema(title = "人员类型")
	@TableField(name = "user_type", searchType = SearchEnums.EQ)
	private String userType;
	/**
	 * 开始时间
	 */
	@Schema(title = "开始时间")
	@JsonFormat(timezone = DateUtils.TIME_ZONE, pattern = DateUtils.YYYY_MM_DD)
	private Date startTime;
	/**
	 * 结束时间
	 */
	@Schema(title = "结束时间")
	@JsonFormat(timezone = DateUtils.TIME_ZONE, pattern = DateUtils.YYYY_MM_DD)
	private Date stopTime;
	/**
	 * 状态
	 */
	@Schema(title = "状态")
	@TableField(name = "status", searchType = SearchEnums.EQ)
	private String status;
	/**
	 * 职责说明
	 */
	@Schema(title = "职责说明")
	private String remark;
	
	private transient String userName;
	private transient String userCode;
	private transient String mobile;
	private transient String email;
	private transient String workNo;
	private transient Collection<String> userIds;
	private transient Collection<String> deptIds;
	
	public SysDeptUser(String deptId, String userId, String userType) {
		this.deptId = deptId;
		this.userId = userId;
		this.userType = userType;
	}
	
	public SysDeptUser(SysDeptUser entity, String userId) {
		this.userId = userId;
		this.deptId = entity.getDeptId();
		this.userType = entity.getUserType();
		this.startTime = entity.getStartTime();
		this.stopTime = entity.getStopTime();
		this.status = entity.getStatus();
		this.remark = entity.getRemark();
	}
	
	@Override
	public void preInsert() {
		super.preInsert();
		if (StringUtils.isBlank(this.userType)) {
			this.userType = DeptPostTypeEnums.OTHER_POST.getCode();
		}
//		if (StringUtils.isBlank(this.status)) {
//			if (DeptPostTypeEnums.SECOND_POST.getCode().equalsIgnoreCase(this.userType)) {
//				this.status = DeptPostTypeEnums.SECOND_POST.getCode();
//			} else if (DeptPostTypeEnums.PART_POST.getCode().equalsIgnoreCase(this.userType)) {
//				this.status = DeptPostTypeEnums.PART_POST.getCode();
//			} else {
//				this.status = DeptPostTypeEnums.T.getCode();
//			}
//		}
		if (this.startTime == null) {
			this.startTime = DateUtils.truncate(new Date(), Calendar.DATE);
		}
	}
	
	@Override
	public void preUpdate() {
		super.preUpdate();
	}
	
	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("userId", getUserId()).append("deptId", getDeptId()).toString();
	}
	
}
